//
//  EventAssignManagerVC.m
//  ConvenientlyTake
//
//  Created by 森鑫 on 2022/12/29.
//

#import "EventAssignManagerVC.h"
#import "JXCategoryTitleView.h"
#import "JXCategoryIndicatorLineView.h"
#import "JXCategoryListContainerView.h"
#import "EvenDetailBottomView.h"
#import "EventAssignDeptVC.h"
#import "EventAssignUserVC.h"

@interface EventAssignManagerVC ()<UIScrollViewDelegate,JXCategoryViewDelegate,EvenDetailBottomViewBtnDelegate>


@property(nonatomic,strong)UIView*topBgView;
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) NSArray <NSString *> *titles;
@property (nonatomic, assign) NSInteger index; // 选择
/** 视图 */
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) EventAssignDeptVC *deptVC;
@property (nonatomic, strong) EventAssignUserVC *userVC;

@property (nonatomic , strong) EvenDetailBottomView *bottomView;

@end

@implementation EventAssignManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"指派";
    [self setupUI];
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    [self setNavStyleWhiteAndBackBtn];
}

- (void)setupUI {
    [self.view addSubview:self.scrollView];
    /**进退场交接*/
    self.categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 12, SCREEN_WIDTH, 54)];
    self.categoryView.backgroundColor = [UIColor whiteColor];
    self.categoryView.titleFont = TGAutoFont(14);
    self.categoryView.titleSelectedFont = TGSemiboldFont(16);
    self.categoryView.titleSelectedColor = RGBA(22, 93, 255, 1);
    self.categoryView.titleColor = RGBA(29, 33, 41, 1);
    self.categoryView.titles = self.titles;
    
 /**线条*/
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorWidth = RatioWidth(26);
    lineView.indicatorHeight = RatioWidth(2);
    lineView.indicatorColor =  RGBA(22, 93, 255, 1);
    self.categoryView.indicators = @[lineView];
    self.categoryView.contentScrollView = self.scrollView;
    self.categoryView.delegate = self;
    
    self.scrollView.scrollEnabled = NO;
    [self.view addSubview:self.categoryView];
    
    
    for (int i = 0; i < self.titles.count; i++) {
        if (i == 0) {
            self.userVC = [[EventAssignUserVC alloc] init];
            self.userVC.deptId = self.deptId;
            self.userVC.eventId = self.eventId;
            self.userVC.assignTaskId = self.assignTaskId;
            self.userVC.showBottomView = NO;
            [self addChildViewController:self.userVC];
            UIView *childView = self.childViewControllers[0].view;
            childView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.scrollView.height);
            [self.scrollView addSubview:childView];
        } else {
            self.deptVC = [[EventAssignDeptVC alloc] init];
            self.deptVC.deptId = self.deptId;
            self.deptVC.eventId = self.eventId;
            self.deptVC.assignTaskId = self.assignTaskId;
            self.deptVC.showBottomView = NO;
            [self addChildViewController:self.deptVC];
            UIView *childView = self.childViewControllers[1].view;
            childView.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, self.scrollView.height);
            [self.scrollView addSubview:childView];
        }
    }
    
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(102);
    }];
    
}


#pragma mark ---------------


/**
 点击选中的情况才会调用该方法

 @param categoryView categoryView对象
 @param index 选中的index
 */
- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index{
    NSLog(@"%ld" , index);
    self.index = index;
}

/**
 滚动选中的情况才会调用该方法

 @param categoryView categoryView对象
 @param index 选中的index
 */
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index{
    NSLog(@"%ld" , index);
    self.index = index;
}

- (void)selelctItemWitIndex:(NSInteger)index andSender:(UIButton *)sender{
    NSLog(@"确定");
    kPreventRepeatClickTime(5);
    if (self.index == 0) {
        [self.userVC commit];
    } else if (self.index == 1){
        [self.deptVC commit];
    }
}


#pragma mark ---------------
#pragma mark 懒加载
- (NSArray<NSString *> *)titles {
    if (!_titles) {
        _titles = @[@"本局委人员", @"县局委"];
    }
    return _titles;
}

- (EvenDetailBottomView *)bottomView{
    if(!_bottomView){
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"EvenDetailBottomView" owner:self options:0] firstObject];
        _bottomView.delegate = self;
        _bottomView.leftBtn.hidden = YES;
        [_bottomView.rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    }
    return _bottomView;
}


- (UIScrollView *)scrollView {
    if (!_scrollView) {
        CGFloat scrollH = SCREEN_HEIGHT - NavBarHeight -102 - 78;
        UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0,78, SCREEN_WIDTH, scrollH)];
        sv.backgroundColor = UIColorForBackground;
        sv.bounces = NO;
        sv.delegate = self;
        sv.pagingEnabled = YES;
        sv.showsHorizontalScrollIndicator = NO;
        sv.showsVerticalScrollIndicator = NO;
        sv.alwaysBounceHorizontal = YES;
        sv.bouncesZoom = NO;
        sv.contentSize = CGSizeMake(SCREEN_WIDTH * self.titles.count, 0);
        [self.view addSubview:sv];
        _scrollView = sv;
    }
    return _scrollView;
}


@end
